<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>交个朋友-首页</title>
		<link rel="stylesheet" type="text/css" href="css/home-style.css"/>
	</head>
	<body>
		
		<!-- 头部 -->
		<div class="head">
			<p class="logout" id="logout">退出登陆</p>
			<img src="img/icon.jpg" class="user-icon" >
			<p class="nickname" id="nickname">昵称:&nbsp;&nbsp;微笑纯属礼貌</p>
			<div class="navigation">
				<a href="#" id="queryAll" class="on">交个朋友圈</a>
				<a href="#" id="queryNick">我的</a>
			</div>
		</div>
		<!-- 底部 -->
		<div class="fart" id="fart">
			<!-- 注解：可以先编写静态的死数据结构，切换动态注解 -->
			<div class="fart-content">
				<a id="delete" >X</a>
				<div class="fart-content-user">
					<img src="img/icon.jpg" class="fart-content-user-icon" >
					<div>
						<p class="fart-content-user-name">微笑纯属礼貌</p>
						<p class="fart-content-user-date">2020-2-02</p>
					</div>
				</div>
				<div class="fart-content-pubish">
					<p>XXXXXXXXXXXXXXXXXXXXXXX</p>
					<img src="img/0.jpg" >
				</div>
			</div>
		</div>
		
		<img src="img/publish.png" class="publish" id="publish" >
	</body>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
//        初始化判断
//        判断当前是否是登录状态 - 如果有值 返回true 如果没有返回false
        if(!sessionStorage.getItem("nickname")){
            window.location.href = "login.html";
        }
//         进入主页 - 就需要访问数据库 - 把数据进行展示到首页上
        function query_data(action,nickname){
            var config = {
                method:"GET",
                url:"/jgpy/publish",
                async:true,
                dataType:"text",
                data:{
                    "action":action,
                    "nickname":nickname
                },
                success:function (data){
                    console.log(data);
                    data = JSON.parse(data);
                    var all_html = "";
                    // {code:200,msg:查询成功,data:[]}
                    if(data.code =="200"){
                        var publish_data = JSON.parse(data.data);
                        for(var i = publish_data.length - 1;i>=0;i--){
                            var item_publish = publish_data[i];
                            if(nickname != "all"){
                                var item_html = "<div class='fart-content'>" +
                                    "<a id='delete' onclick='delete_publish("+item_publish.nSign+")'>X</a>" +
                                    "<a id='update'>U</a>" +
                                    "<div class='fart-content-user'>" +
                                    "<img src='img/icon.jpg' class='fart-content-user-icon' >" +
                                    "<div>" +
                                    "<p class='fart-content-user-name'>"+item_publish.nNickname+"</p>" +
                                    "<p class='fart-content-user-date'>"+item_publish.nCreatetime+"</p>" +
                                    "</div>" +
                                    "</div>" +
                                    "<div class='fart-content-pubish'>" +
                                    "<p>"+item_publish.nContentText+"</p>" +
                                    "<img src='img/"+item_publish.nContentImg+"' >" +
                                    "</div>" +
                                    "</div>" +
                                    ""
                            }else {
                                var item_html = "<div class='fart-content'>" +
                                    "<div class='fart-content-user'>" +
                                    "<img src='img/icon.jpg' class='fart-content-user-icon' >" +
                                    "<div>" +
                                    "<p class='fart-content-user-name'>"+item_publish.nNickname+"</p>" +
                                    "<p class='fart-content-user-date'>"+item_publish.nCreatetime+"</p>" +
                                    "</div>" +
                                    "</div>" +
                                    "<div class='fart-content-pubish'>" +
                                    "<p>"+item_publish.nContentText+"</p>" +
                                    "<img src='img/"+item_publish.nContentImg+"' >" +
                                    "</div>" +
                                    "</div>" +
                                    ""
                            }

                            all_html+=item_html;
                        }
                    //     把代码写入到网页中
                        $("#fart").html(all_html);
                    }else {
                        alert(data.msg);
                        $("#fart").html("");

                    }

                }
            }
            $.ajax(config);

        }
//         默认调用-
            query_data("查询所有","all");
//        【1】发布文章按钮
            $("#publish").click(function (){
                window.location.href = "publish.html";
            })
    // 【2】设置一下当前home页面 - 显示昵称
            var nickName = sessionStorage.getItem("nickname");
            $("#nickname").html("昵称："+nickName)
    // 【3】切换选项卡 - 先修改状态栏
            $(".navigation").find("a").click(function (){
                $(".navigation a").removeClass("on");
                $(this).addClass("on");
            })
    // 【4】退出功能
            $("#logout").click(function (){
                sessionStorage.removeItem("nickname");
                window.location.href = "login.html";
            })
    // 点击我的
    $("#queryNick").click(function (){
        query_data("查询个人",sessionStorage.getItem("nickname"));

    })
    // 朋友圈
    $("#queryAll").click(function (){
        query_data("查询所有","all");
    })

    // 删除的方法
    function delete_publish(sign){
        console.log(sign)
        var config = {
            method:"POST",
            url:"/jgpy/publish",
            async:true,
            dataType:"text",
            data:{
                "action":"删除",
                "sign":sign
            },
            success:function (data){
                data = JSON.parse(data);
                if(data.code = "200"){
                    alert(data.msg);
                    window.location.reload();
                }else {
                    alert(data.msg);
                }

            }

        }
        $.ajax(config);

    }



    </script>
	
</html>
